import { Controller, Get, Post, Body, Patch, Param, Delete, Logger, Query, UseFilters, UseGuards } from '@nestjs/common';
import { UserService } from './user.service';
import { TypeormFilter } from 'src/filters/typeorm.filter';
import { User } from './entities/user.entity';
import { CreateUserPipe } from './pipes/create-user/create-user.pipe';
import { AuthGuard } from '@nestjs/passport';
import { AdminGuard } from 'src/guards/admin/admin.guard';

@Controller('user')
@UseFilters(new TypeormFilter())  // 注入 typeOrm出错的过滤器
// @UseGuards( AuthGuard('jwt'))  // user模块 整个加上登录鉴权
export class UserController {
    // 注册日志方法  参数 当前控制器的name
    private logger = new Logger(UserController.name)
    constructor(private readonly userService: UserService) { 
        // 调用构造函数时打印一次
        this.logger.log('UserController 初始化完成')
    }

    @Post('/create')
    // @Body的参数 放置过滤器  如果没有过滤器 会有bug 自动添加测试的角色
    create(@Body(CreateUserPipe) createUserDto: User) {
        return this.userService.create(createUserDto);
    }

    @Get('/all')
    // UseGuards 可以传递多个守卫
    @UseGuards( AuthGuard('jwt') , AdminGuard) 
    findAll(@Query() query: UserQuery):any {
        return this.userService.findAll(query);
    }


    @Get(':id')
    findOne(@Param('id') id: string) {
        return this.userService.findOne(id);
    }

    @Patch('/update/:id')
    @UseGuards(AuthGuard('jwt'))  // 设置守卫  // 校验请求头 是否携带token
    update(@Param('id') id: string, @Body() updateUserDto: User) {
        return this.userService.update(id, updateUserDto);
    }

    @Delete('/delete/:id')
    remove(@Param('id') id: string) {
        return this.userService.remove(id);
    }

    @Get('/profile/:id')
    findProfile(@Param('id') id :string){
        return this.userService.findProfile(id)
    }

    @Get('/logs/:id')
    findLogs(@Param('id') id :string){
        return this.userService.findLogs(id)
    }

    // 
    @Get('/group-logs/:id')
    findGroupLogs(@Param('id') id :string){
        return this.userService.findLogsByGroup(id)
    }
}
